library(text)

train_path <- "/Users/yuwang/Desktop/PMethods/Regression-Anxiety/train_dev.csv"
test_path <- "/Users/yuwang/Desktop/PMethods/Regression-Anxiety/test.csv"

# Read the CSV file into a data frame
train <- read.csv(train_path)
test <- read.csv(test_path)

# Record start time
start_time <- Sys.time()
train_word_embeddings <- textEmbed(
  train,
  model = "roberta-base", aggregation_from_layers_to_tokens = "concatenate", aggregation_from_tokens_to_texts = "mean", keep_token_embeddings = FALSE
)
end_time <- Sys.time()
# Calculate duration
duration <- end_time - start_time
# Print the duration
cat("Execution duration for train embedding:", duration, "\n")

# Completed layers output for text_long (variable: 1/1, duration: 18.452731 mins).
# Completed layers aggregation for word_type_embeddings. 
# Completed layers aggregation (variable 1/1, duration: 32.300368 mins).
# > end_time <- Sys.time()
# > # Calculate duration
#   > duration <- end_time - start_time
# > # Print the duration
#   > cat("Execution duration for train embedding:", duration, "\n")
# Execution duration for train embedding: 51.31775 

saveRDS(train_word_embeddings, "/Users/yuwang/Desktop/PMethods/Regression-Anxiety/supp_train_word_embeddings.rds")


# Record start time
start_time <- Sys.time()
test_word_embeddings <- textEmbed(
  test,
  model = "roberta-base", aggregation_from_layers_to_tokens = "concatenate", aggregation_from_tokens_to_texts = "mean", keep_token_embeddings = FALSE
)
end_time <- Sys.time()
# Calculate duration
duration <- end_time - start_time
# Print the duration
cat("Execution duration for test embedding:", duration, "\n")


# Completed layers output for text_long (variable: 1/1, duration: 3.251263 mins).
# Completed layers aggregation for word_type_embeddings. 
# Completed layers aggregation (variable 1/1, duration: 7.071943 mins).
# > end_time <- Sys.time()
# > # Calculate duration
#   > duration <- end_time - start_time
# > # Print the duration
#   > cat("Execution duration for test embedding:", duration, "\n")
# Execution duration for test embedding: 10.39181 

saveRDS(test_word_embeddings, "/Users/yuwang/Desktop/PMethods/Regression-Anxiety/supp_test_word_embeddings.rds")

train_word_embeddings = readRDS("/Users/yuwang/Desktop/PMethods/Regression-Anxiety/supp_train_word_embeddings.rds")
test_word_embeddings = readRDS("/Users/yuwang/Desktop/PMethods/Regression-Anxiety/supp_test_word_embeddings.rds")

for (i in c(500, 1000, 2000)){
  # Record start time
  start_time <- Sys.time()
  model <- textTrain(
    x = train_word_embeddings$texts$text_long[1:i,], # the predictor variables (i.e., the word embeddings)
    y = train$anxiety[1:i], # the criterion variable (i.e., the rating scale score.)
    force_train_method = "regression"
  )
  end_time <- Sys.time()
  # Calculate duration
  duration <- end_time - start_time
  # Print the duration
  cat("Training size:", i, "\n")
  print(duration)
  cat("Execution duration for training:", duration, "\n")
  
  predictions <- textPredict(model, word_embeddings = test_word_embeddings$texts)
  
  # Calculate Pearsonr
  correlation <- cor(predictions$text_long__ypred, test$anxiety, method = "pearson")
  cat("Correlation", correlation, "\n")
  # Calculate RMSE
  errors <- predictions$text_long__ypred - test$anxiety
  squared_errors <- errors^2
  mean_squared_error <- mean(squared_errors)
  rmse <- sqrt(mean_squared_error)
  cat("rmse", rmse, "\n")
}